package com.mingqijia.gassafety.authority.dal.handler;

import com.mingqijia.gassafety.authority.api.req.role.CreateRoleReqDTO;
import com.mingqijia.gassafety.authority.api.req.role.QueryRoleReqDTO;
import com.mingqijia.gassafety.authority.api.req.role.UpdateRoleReqDTO;
import com.mingqijia.gassafety.authority.api.resp.role.RoleInfoRespDTO;
import com.mingqijia.gassafety.authority.dal.command.CreateRoleCommand;
import com.mingqijia.gassafety.authority.dal.command.QueryRoleCommand;
import com.mingqijia.gassafety.authority.dal.command.RemoveRoleCommand;
import com.mingqijia.gassafety.authority.dal.command.UpdateRoleCommand;
import com.mingqijia.gassafety.authority.dal.handler.RoleTranslatorImpl;
import com.mingqijia.gassafety.db.entity.auth.RoleEntry;
import com.mingqijia.gassafety.shared.utils.MappingTools;
import org.mapstruct.Mapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 对象转换接口
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月09日 20:37
 */
@Mapper(componentModel = "spring", uses = MappingTools.class)
@ConditionalOnMissingBean(RoleTranslatorImpl.class)
public interface RoleTranslator {

    RoleInfoRespDTO toRespDTO(RoleEntry entry);

    RoleEntry toEntry(CreateRoleCommand command);

    RoleEntry toEntry(UpdateRoleCommand command);

    QueryRoleCommand toCommand(QueryRoleReqDTO req);

    CreateRoleCommand toCommand(CreateRoleReqDTO req);

    UpdateRoleCommand toCommand(UpdateRoleReqDTO req);
    
    RoleEntry toEntry(RemoveRoleCommand command);

    default List<RoleInfoRespDTO> toRespDTO(List<RoleEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream().map(this::toRespDTO)
                .collect(Collectors.toList());
    }
}
